#include "mainwindow.h"

#include <qtdbusobjectmanager.h>
#include <qtdbusinterfacelocation.h>

#include <QPushButton>
#include <QDBusConnection>
#include <QMenuBar>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    QPushButton *pushButton = new QPushButton{this};
    pushButton->move(100, 100);
    resize(300, 300);

    QDBusConnection connection = QDBusConnection::sessionBus();
    connection.registerService("com.examples.QtDBusObjectManager");
    manager = new QtDBusObjectManager{this};
    manager->registerPath(connection, "/QtDBusObjectManager");

    manager->registerObject({}, {}, pushButton);
    manager->registerObject("/window", {}, this);

    qDebug() << connection.connect("", "", "org.freedesktop.DBus.ObjectManager", "InterfacesAdded", this, SLOT(InterfacesAdded(QDBusObjectPath,QMap<QString,QVariantMap>)));
    qDebug() << connection.connect("", "", "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved", this, SLOT(InterfacesRemoved(QDBusObjectPath,QStringList)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::registerService()
{
    manager->registerObject("service", "menuBar", menuBar());
}

void MainWindow::unregisterService()
{
    manager->unregisterObject(menuBar());
}

void MainWindow::InterfacesAdded(const QDBusObjectPath &path, const QMap<QString, QVariantMap> &interfaceInfos)
{
    qDebug() << __func__ << path.path() << interfaceInfos;
}

void MainWindow::InterfacesRemoved(const QDBusObjectPath &path, const QStringList &interfaces)
{
    qDebug() << __func__ << path.path() << interfaces;
}
